package com.rslai.commons.validator.jsonvalidator;

import java.util.List;

/**
 * 枚举类，将JsonNode类型转换为JSON规范中定义的类型
 */
public enum TYPE {

    OBJECT("object", Object.class),
    ARRAY("array", List.class),
    STRING("string", String.class),
    NUMBER("number", Number.class),
    INTEGER("integer", Integer.class),
    INT("int", Integer.class),
    BOOLEAN("boolean", Boolean.class),
    NULL("null", null),
    ANY("any", null);

    private String type;
    private Class clazz;

    /**
     * 构造方法
     * @param type type
     * @param clazz class
     */
    private TYPE(String type, Class clazz) {
        this.type = type;
        this.clazz = clazz;
    }

    /**
     * 返回clazz
     * @return clazz
     */
    public Class getClazz() {
        return clazz;
    }

    /**
     * 根据type名称返回TYPE
     * @param typeName type名称
     * @return TYPE
     */
    public static TYPE fromType(String typeName) {
        for (TYPE type : values()) {
            if (type.type.equals(typeName)) {
                return type;
            }
        }
        return null;
    }

    /**
     * toString方法
     * @return String
     */
    @Override
    public String toString() {
        return String.format("type=[%s],class=[%s]", type, clazz);
    }

}
